import express, { Request, Response } from 'express'
import { v4 as uuid } from 'uuid'
import { token, parseToken } from './src/utils/jwt'
import { Ok, Fail } from './src/utils/commonResponse'
import { PORT } from './src/utils/contant'
import { tokenParams } from './global'
import loginRouter from './src/route/login'
import userRouter from './src/route/user'
import './src/utils/redis'

const app = express()
app.use(express.static('./src/public'))
app.use(loginRouter)
app.use('/user', userRouter)
interface IRoute {
    name: string
    path: string
    component: string
    children: IRoute[]
}

app.get('/', (req: Request, res: Response) => {
    const params: tokenParams = { id: 1, date: (new Date()).getTime() }
    Ok<{ params: tokenParams, uid: string }>(res, { params, uid: uuid() })
})

app.get('/parse', (req: Request, res: Response) => {
    Ok<tokenParams>(res, parseToken())
})

app.get('/test', (req: Request, res: Response) => {
        const route: Array<IRoute> = [
            { name: 'Test', path: '/test', component: 'Test/Test', children: [
                    { name: 'Count', path: 'count', component: 'Test/Count', children: [] },
                    { name: 'Life', path: 'life', component: 'Life/Life', children: [] }
                ]
            }
        ]
    Ok<Array<IRoute>>(res, route)
})

app.listen(PORT, () => {
    console.log(`http://localhost:${PORT}`)
})